package generator;

import sudoku.DifficultyLevel;
import sudoku.GameMode;
import sudoku.Options;
import sudoku.Sudoku2;
import th.android.sudoku.Sudo;
import th.android.sudoku.ThSudokuGenerator;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class SqliteGenerator {

    private Connection mConnection;

    public SqliteGenerator() throws ClassNotFoundException, SQLException {
        Class.forName("org.sqlite.JDBC");
        mConnection = DriverManager.getConnection("jdbc:sqlite:sudoku.db");
    }

    public boolean fillWithEasyOne() throws SQLException {
        ThSudokuGenerator generator = new ThSudokuGenerator();
        Sudo sudo = generator.extremeEasyOne();
        if (sudo == null) return false;
        StringBuilder builder1 = new StringBuilder();
        StringBuilder builder2 = new StringBuilder();
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                builder1.append(generator.mGeneratedSudoku[i][j]);
                builder2.append(generator.mFilledSudoKu[i][j]);
            }
        }
        String origin = builder1.toString();
        String completed = builder2.toString();
        Statement statement = mConnection.createStatement();
        String sql = "insert into data (status, origin, modified, completed, note, last_play, difficulty_level) values ("
                + "0" + ","
                + "\'" + origin + "\'" + ","
                + "\'" + origin + "\'" + ","
                + "\'" + completed + "\'" + ","
                + "\'" + "" + "\'" + ","
                + System.currentTimeMillis() + ","
                +"1"
                + ")";
        statement.execute(sql);
//        for (int i = 0; i < 81; i++) {
//            System.out.print(origin.charAt(i));
//            System.out.print(",");
//            if (i % 9 == 8) {
//                System.out.print("\n");
//            }
//        }
//        System.out.print("\n");
//        for (int i = 0; i < 81; i++) {
//            System.out.print(completed.charAt(i));
//            System.out.print(",");
//            if (i % 9 == 8) {
//                System.out.print("\n");
//            }
//        }
        return true;
    }

    public boolean fillWithSimpleOne() throws SQLException{
        BackgroundGenerator generator = new BackgroundGenerator();
        DifficultyLevel level = Options.getInstance().getDifficultyLevel(1);
        StringBuilder builder1 = new StringBuilder();
        StringBuilder builder2 = new StringBuilder();
        Sudoku2 sudoku = generator.generate(GameMode.PLAYING, level);
        if (sudoku == null) return false;
        for (int i = 0; i < 81; i ++) {
            builder1.append(sudoku.getValues()[i]);
            builder2.append(sudoku.completed[i]);
        }
        String origin = builder1.toString();
        String completed = builder2.toString();
        Statement statement = mConnection.createStatement();
        String sql = "insert into data (status, origin, modified, completed, note, last_play, difficulty_level) values ("
                + "0" + ","
                + "\'" + origin + "\'" + ","
                + "\'" + origin + "\'" + ","
                + "\'" + completed + "\'" + ","
                + "\'" + "" + "\'" + ","
                + System.currentTimeMillis() + ","
                +"2"
                + ")";
        statement.execute(sql);
        return true;
    }

    public boolean fillWithMiddleOne() throws SQLException{
        BackgroundGenerator generator = new BackgroundGenerator();
        DifficultyLevel level = Options.getInstance().getDifficultyLevel(2);
        StringBuilder builder1 = new StringBuilder();
        StringBuilder builder2 = new StringBuilder();
        Sudoku2 sudoku = generator.generate(GameMode.PLAYING, level);
        if (sudoku == null) return false;
        for (int i = 0; i < 81; i ++) {
            builder1.append(sudoku.getValues()[i]);
            builder2.append(sudoku.completed[i]);
        }
        String origin = builder1.toString();
        String completed = builder2.toString();
        Statement statement = mConnection.createStatement();
        String sql = "insert into data (status, origin, modified, completed, note, last_play, difficulty_level) values ("
                + "0" + ","
                + "\'" + origin + "\'" + ","
                + "\'" + origin + "\'" + ","
                + "\'" + completed + "\'" + ","
                + "\'" + "" + "\'" + ","
                + System.currentTimeMillis() + ","
                +"3"
                + ")";
        statement.execute(sql);
        return true;
    }

    public boolean fillWithDifficultyOne() throws SQLException{
        BackgroundGenerator generator = new BackgroundGenerator();
        DifficultyLevel level = Options.getInstance().getDifficultyLevel(3);
        StringBuilder builder1 = new StringBuilder();
        StringBuilder builder2 = new StringBuilder();
        Sudoku2 sudoku = generator.generate(GameMode.PLAYING, level);
        if (sudoku == null) return false;
        for (int i = 0; i < 81; i ++) {
            builder1.append(sudoku.getValues()[i]);
            builder2.append(sudoku.completed[i]);
        }
        String origin = builder1.toString();
        String completed = builder2.toString();
        Statement statement = mConnection.createStatement();
        String sql = "insert into data (status, origin, modified, completed, note, last_play, difficulty_level) values ("
                + "0" + ","
                + "\'" + origin + "\'" + ","
                + "\'" + origin + "\'" + ","
                + "\'" + completed + "\'" + ","
                + "\'" + "" + "\'" + ","
                + System.currentTimeMillis() + ","
                +"4"
                + ")";
        statement.execute(sql);
        return true;
    }

    public boolean fillWithExpertOne() throws SQLException{
        BackgroundGenerator generator = new BackgroundGenerator();
        DifficultyLevel level = Options.getInstance().getDifficultyLevel(5);
        StringBuilder builder1 = new StringBuilder();
        StringBuilder builder2 = new StringBuilder();
        Sudoku2 sudoku = generator.generate(GameMode.PLAYING, level);
        if (sudoku == null) return false;
        for (int i = 0; i < 81; i ++) {
            builder1.append(sudoku.getValues()[i]);
            builder2.append(sudoku.completed[i]);
        }
        String origin = builder1.toString();
        String completed = builder2.toString();
        Statement statement = mConnection.createStatement();
        String sql = "insert into data (status, origin, modified, completed, note, last_play, difficulty_level) values ("
                + "0" + ","
                + "\'" + origin + "\'" + ","
                + "\'" + origin + "\'" + ","
                + "\'" + completed + "\'" + ","
                + "\'" + "" + "\'" + ","
                + System.currentTimeMillis() + ","
                +"5"
                + ")";
        statement.execute(sql);
        return true;
    }

    public void destroy() throws SQLException{
        if (mConnection != null && !mConnection.isClosed()) {
            mConnection.close();
        }
    }

    public static void main(String[] args) {
        SqliteGenerator generator = null;
        try {
            generator = new SqliteGenerator();
            int count = 1800;
            for (int i = 0; i < count;) {
                if (generator.fillWithExpertOne()) {
                    i++;
                    System.out.println("i = " + i);
                }
            }
            System.out.println("completed");
        }catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (generator != null) {
                    generator.destroy();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}
